////////////////////////////////////////
// flightUtil.cpp
// handling an array of objects of class 'Flight'

#include <iostream>
#include <fstream>
#include <string>
#include "Flight.h"
#include "flightUtil.h"
using namespace std;

////////////////////////////////////////
// 'readFlights' reads flight info from file 'flightFile'
//     into array 'flights' of objects of class Flight
// SENTINEL in airline position terminates the info
// returns number of read flights
// exits program if 'flightFile' not opened

int readFlights(Flight flights[], ifstream & flightFile)
{
    string      airline, orig,  dest;
    int         nFlights = 0, flightNum, seats;
    float       price;
    
    if(flightFile)
    {
    
        flightFile >> airline;
        upperCase(airline);

        while(airline != SENTINEL)
        {
            flightFile >> flightNum;
            flightFile >> orig;
            upperCase(orig);
            flightFile >> dest;
            upperCase(dest);
            flightFile >> price;
            flightFile >> seats;
            nFlights++;
        
            flights[nFlights - 1].set(airline, flightNum, orig, dest, price, seats);
        
            flightFile >> airline;
        }
    }
    else
    {
        cerr << "Input file failed to open\n";
        exit(1);
    }
    
    return nFlights;
}

////////////////////////////////////////
// 'printFlight' displays one flight information on one line

void printFlight(const Flight & flight)
{
    cout << "Airline Code: " << flight.get_airline() << "  Flight Number: "
         << flight.get_flightNum() << "  Origination: " << flight.get_orig() 
         << "  Destination: " << flight.get_dest()
         << "  Price: " << flight.get_price()
         << "  # of Seats:" << flight.get_seats() << endl; 
}

////////////////////////////////////////
// 'printFlights' displays flight information in array 'flights'
//     given indices into 'flights' in array 'flightIdx'
//     with 'nIdx' indices

void printFlights(int flightIdx[], int nIdx, Flight flights[])
{
    for(int index = 0; index < nIdx; index++)
    { 
        printFlight(flights[flightIdx[index]]);            
    }
}

////////////////////////////////////////
// 'searchFlights' searches for flights matching 'orig' and 'dest',
// stores indices of matching flights in 'flightIdx'
// return number of matching flights

int searchFlights(int flightIdx[],
    Flight flights[], int nFlights,
    const string & orig, const string & dest)
{
    int index = 0, nIdx = 0;
    
    for(index = 0; index < nFlights; index++)
    {
        if(flights[index].get_orig() == orig && flights[index].get_dest() == dest)
        {
            flightIdx[nIdx] = index;
            nIdx++;
        }
    }
    
    return nIdx;
}

////////////////////////////////////////
// 'searchFlight' searches for flight given 'airline' and 'flightNum'
//     in array 'flights' with 'nFlights' flights
// return index into 'flights' of matching flight
// or -1 if not found

int searchFlight(Flight flights[], int nFlights,
    const string & airline, int flightNum)
{
    int indexMatch = -1;
    
    for(int index = 0; index < nFlights; index++)
    {
        if(flights[index].get_airline() == airline &&
                flights[index].get_flightNum() == flightNum)
        {
           indexMatch = index;
           return indexMatch;
        }
    }
    
    return indexMatch;
}

////////////////////////////////////////
// 'sortFlightIdx' sorts indices 'flightIdx' with 'nIdx' indices,
//     indices are into array 'flights'
// sorting is in increasing order by price of flights
// bubble sort used

void sortFlightIdx(int flightIdx[], int nIdx, Flight flights[])
{
    int idx1, idx2;

    for (int i = nIdx - 1; i >= 0; i--)
        for (int k = 0; k < i; k++)
        {
            idx1 = flightIdx[k]; 
            idx2 = flightIdx[k + 1]; 
            if (flights[idx1].get_price() > flights[idx2].get_price())
                swap(flightIdx[k], flightIdx[k + 1]);
        }
}

////////////////////////////////////////
// 'bookFlight' books 'nSeats' seats on 'flight'
// updates remaining seats (if successful)
// returns true if successful
// returns false if not enough seats

bool bookFlight(Flight & flight, int nSeats)
{
    if(flight.get_seats() >= nSeats)
    {
        flight.set_seats((flight.get_seats() - nSeats));
        return true;
    }
    else
        return false;
}
